package mipo.top.system.service.impl;

import mipo.top.admin.domain.Department;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.system.domain.Menu;
import mipo.top.system.service.IMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenuServiceimpl extends BaseServiceimpl<Menu> implements IMenuService {

    @Override
    public List<Menu> treeData() {
        List<Menu> menus = super.findall();
        Map<Long, Menu> menuMap = menus.stream().collect(Collectors.toMap(Menu::getId, e -> e));
        List<Menu> treeData = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParentId()== null){
                treeData.add(menu);
            }else {
                Menu sd = menuMap.get(menu.getParentId());
                if(sd != null) {
                    sd.getChildren().add(menu);
                }
            }
        }
        return treeData;
    }

    @Override
    public List<Menu> fristData() {
        List<Menu> menus = super.findall();
        return menus.stream().filter(m->null==m.getParentId()).collect(Collectors.toList());
    }
}
